package org.example.cake.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 文晓平
 * @version 1.0
 * @Description TODO
 * @date 2025/2/25 9:15
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    //目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //获取请求的路径
//        String requestURI = request.getRequestURI();
//        log.info("请求的路径是:{}", requestURI);
//        //做登录拦截
//        Object userName = request.getSession().getAttribute("userName");
//        if(userName != null) {
//            return true;//放行
//        }
//        //重定向到登录页
//        response.sendRedirect(request.getContextPath() + "/login");
//        return false;
        return true;
    }

    //目标方法执行之后
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("目标方法执行之后{}",modelAndView);
    }

    //数据渲染之后
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("目标方法执行之后异常",ex);
    }
}
